package com.aierkeji.b2c.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aierkeji.b2c.service.AkjOrderService;

@RestController
@RequestMapping(value = "order")
public class AkjOrderController extends BaseController {
    private final Logger log = LogManager.getLogger(getClass());

	@Autowired
	private AkjOrderService akjOrderService;

	/**
	 * 
	 * @param productId
	 * @param salesId
	 * @return
	 */
	@GetMapping("/getConfig/{productId}/{salesId}")
	public Map<String, Object> getConfig(@PathVariable("productId") String productId,
			@PathVariable("salesId") String salesId,HttpServletRequest request) {
		return akjOrderService.getPageConfig(productId, salesId);
	}

	/**
	 * 获取预授权页面参数
	 * 
	 * @param productId
	 * @param salesId
	 * @return
	 */
	@PostMapping("/getLicensing/{productId}")
	public Map<String, Object> getLicensing(@PathVariable("productId") String productId,@RequestBody Map<String,Object> params,HttpServletRequest request) {
		log.info("params:{}", params);
		return akjOrderService.getLicensing(productId,request,params);
	}

	/**
	 * 支付宝预授权完成保存订单
	 * @param request
	 * @return
	 */
	@PostMapping("/tradePaySuccess")
	public Map<String, Object> tradePaySuccess(@RequestBody Map<String,Object> params,HttpServletRequest request) {
		Map<String, Object> map = akjOrderService.tradePaySuccess(params,request);
		return map;
	}
	
	
	/**
	 * 
	 * @return
	 */
	/**
	 * 支付宝预授权取消作废订单
	 * @param request
	 * @return
	 */
	@PostMapping("/tradePayCancel")
	public Map<String, Object> tradePayCancel(@RequestBody Map<String,Object> params,HttpServletRequest request) {
		Map<String, Object> map = akjOrderService.tradePayCancel(params,request);
		return map;
	}
	
	@PostMapping("findByOrderNo")
	public Map<String,Object> findByOrderNo(@RequestBody Map<String,Object> params){
		Map<String, Object> map = akjOrderService.findByOrderNo(params);
		return map;
	}
	
	@PostMapping("getYZM/{id}")
	public Map<String,Object> getYZM(@PathVariable(name="id",required=true)String id,HttpServletRequest request,@RequestBody Map<String,Object> map){
		return akjOrderService.getYZM(id,request,map);
		
	}
	
	@PostMapping("cancelByQuery/{id}")
	public Map<String,Object> cancelByQuery(@PathVariable(name="id",required=true)String id,HttpServletRequest request,@RequestBody Map<String,Object> params){
		Map<String, Object> map = akjOrderService.cancel(id,request,params);
		return map;
	}
}
